/********************************************************************
	Rhapsody	: 7.5 
	Login		: KBE
	Component	: DefaultComponent 
	Configuration 	: CodeConfig
	Model Element	: Reactor
//!	Generated Date	: Wed, 19, Sep 2012  
	File Path	: DefaultComponent\CodeConfig\Reactor.cpp
*********************************************************************/

//## auto_generated
#include "Reactor.h"
//## dependency SimulatorReactorImplementation
#include "SimulatorReactorImplementation.h"
//## dependency SocketReactorImplementation
#include "SocketReactorImplementation.h"
//itsReactorImplementation->registerHanler(h, eh, et);

//## package Default

//## class Reactor
Reactor* Reactor::reactor_impl_;

void Reactor::handleEvents(int time) {
    //#[ operation handleEvents(int)
    //#]
}

Reactor * Reactor::instance() {
    //#[ operation instance()
    if (reactor_impl_ == 0)
    #ifdef _SIMULATOR
    	reactor_impl_ = new SimulatorReactorImplementation();   
    #else
    	reactor_impl_ = new SocketReactorImplementation();   
    #endif
    return reactor_impl_;
    //#]
}

void Reactor::registerHandler(EventHandler& eh, const Event_Type& et) {
    //#[ operation registerHandler(EventHandler,Event_Type)
    eh.setType(et);
    addItsEventHandler(eh.getHandle(), &eh);  
    //#]
}

void Reactor::removeHandler(EventHandler& eh, const Event_Type& et) {
    //#[ operation removeHandler(EventHandler,Event_Type)
    removeItsEventHandler(&eh);
    //#]
}

Reactor::Reactor() : itsEventHandler() {
    //#[ operation Reactor()
    reactor_impl_ = 0;
    
    //#]
}

void Reactor::registerHandler(HANDLE h, EventHandler& eh, const Event_Type& et) {
    //#[ operation registerHandler(HANDLE,EventHandler,Event_Type)
     eh.setHandle(h);
     eh.setType(et);
     addItsEventHandler(h, &eh);       
    //#]
}

void Reactor::removeHandler(HANDLE h, const Event_Type& et) {
    //#[ operation removeHandler(HANDLE,Event_Type)
    EventHandler *eh = getItsEventHandler(h); 
    removeItsEventHandler(eh);
    //#]
}

Reactor::~Reactor() {
    cleanUpRelations();
}

void Reactor::setReactor_impl_(Reactor* p_reactor_impl_) {
    reactor_impl_ = p_reactor_impl_;
}

std::map<HANDLE, EventHandler*>::const_iterator Reactor::getItsEventHandler() const {
    std::map<HANDLE, EventHandler*>::const_iterator iter;
    iter = itsEventHandler.begin();
    return iter;
}

std::map<HANDLE, EventHandler*>::const_iterator Reactor::getItsEventHandlerEnd() const {
    return itsEventHandler.end();
}

void Reactor::removeItsEventHandler(EventHandler* p_EventHandler) {
    if(p_EventHandler != NULL)
        {
            p_EventHandler->__setItsReactor(NULL);
        }
    _removeItsEventHandler(p_EventHandler);
}

void Reactor::clearItsEventHandler() {
    std::map<HANDLE, EventHandler*>::const_iterator iter;
    iter = itsEventHandler.begin();
    while (iter != itsEventHandler.end()){
        ((*iter).second)->_clearItsReactor();
        iter++;
    }
    _clearItsEventHandler();
}

void Reactor::cleanUpRelations() {
    {
        std::map<HANDLE, EventHandler*>::const_iterator iter;
        iter = itsEventHandler.begin();
        while (iter != itsEventHandler.end()){
            Reactor* p_Reactor = ((*iter).second)->getItsReactor();
            if(p_Reactor != NULL)
                {
                    ((*iter).second)->__setItsReactor(NULL);
                }
            iter++;
        }
        itsEventHandler.clear();
    }
}

void Reactor::_removeItsEventHandler(EventHandler* p_EventHandler) {
    std::map<HANDLE, EventHandler*>::iterator pos = std::find_if(itsEventHandler.begin(), itsEventHandler.end(),OMValueCompare<const HANDLE,EventHandler*>(p_EventHandler));
    if (pos != itsEventHandler.end()) {
    	itsEventHandler.erase(pos);
    }
}

void Reactor::_clearItsEventHandler() {
    itsEventHandler.clear();
}

Reactor* Reactor::getReactor_impl_() {
    return reactor_impl_;
}

void Reactor::addHandle(HANDLE handle, const Event_Type& type) {
    //#[ operation addHandle(HANDLE,Event_Type)
    //#]
}

void Reactor::createHandleList() {
    //#[ operation createHandleList()
      std::map<HANDLE, EventHandler*>::const_iterator iterator = itsEventHandler.begin();
      while (iterator != itsEventHandler.end())
      {   
        EventHandler *eh = (*iterator).second;
      	addHandle(eh->getHandle(), eh->getType()); 
      	iterator++;
      } 
    //#]
}

EventHandler* Reactor::getItsEventHandler(HANDLE key) const {
    return (itsEventHandler.find(key) != itsEventHandler.end() ? (*itsEventHandler.find(key)).second : NULL);
}

void Reactor::addItsEventHandler(HANDLE key, EventHandler* p_EventHandler) {
    if(p_EventHandler != NULL)
        {
            p_EventHandler->_setItsReactor(this);
        }
    _addItsEventHandler(key, p_EventHandler);
}

void Reactor::removeItsEventHandler(HANDLE key) {
    EventHandler* p_EventHandler = getItsEventHandler(key);
    if(p_EventHandler != NULL)
        {
            p_EventHandler->_setItsReactor(NULL);
        }
    _removeItsEventHandler(key);
}

std::map<HANDLE, EventHandler*>::const_iterator Reactor::getIteratorEventHandler() const {
    return iteratorEventHandler;
}

void Reactor::setIteratorEventHandler(std::map<HANDLE, EventHandler*>::const_iterator p_iteratorEventHandler) {
    iteratorEventHandler = p_iteratorEventHandler;
}

void Reactor::_addItsEventHandler(HANDLE key, EventHandler* p_EventHandler) {
    itsEventHandler.insert(std::map<HANDLE, EventHandler*>::value_type(key, p_EventHandler));
}

void Reactor::_removeItsEventHandler(HANDLE key) {
    itsEventHandler.erase(key);
}

/*********************************************************************
	File Path	: DefaultComponent\CodeConfig\Reactor.cpp
*********************************************************************/
